$import("com.joypard.platform.organization.job.JobDI");
/**
 * 职位视图
 */
K.defView("com.joypard.platform.organization.job.JobView", {
	organizatoinView : null, // 构造参数

	/**
	 * 权限配置
	 */
	purviewConfig : {
		view : "platform.organization.job",
		addJob : "platform.organization.job.addJob",
		updateJob : "platform.organization.job.updateJob",
		delJob : "platform.organization.job.delJob"
	},

	/**
	 * 构造方法
	 * 
	 * @param organizatoinView
	 *            组织视图
	 */
	JobView : function(organizatoinView) {
		this.organizatoinView = organizatoinView;
		E.addListener("组织机构树-单击", this.onClickOrgTreeNode, this);
		E.addListener("职位-新增-成功", this.onAddJobSuccess, this);
		E.addListener("职位-修改-成功", this.onUpdateJobSuccess, this);
		E.addListener("职位-删除-成功", this.onDelJobSuccess, this);
	},

	/**
	 * 职位面板
	 */
	createJobPanel : function() {
		var options = {
			title : "职位信息",
			iconCls : "p-tab",
			tbar : {
				xtype : "toolbar",
				hidden : !JobView.purviewUpdateJob() && !JobView.purviewAddJob() && !JobView.purviewDelJob(),
				items : [ {
					text : "新增",
					hidden : !JobView.purviewAddJob(),
					iconCls : 'add',
					handler : K.createDelegate(this.onAddJob, this)
				}, {
					text : "修改",
					hidden : !JobView.purviewUpdateJob(),
					iconCls : 'update',
					handler : K.createDelegate(this.onUpdateJob, this)
				}, {
					text : "删除",
					hidden : !JobView.purviewDelJob(),
					iconCls : 'delete',
					handler : K.createDelegate(this.onDelJob, this)
				} ]
			},
			defaults : {
				width : "100%",
				headerCssClass : "x-panel-header-no-border-t-l-r",
				bodyCssClass : "x-panel-body-no-border-t-l"
			},
			layout : "vbox"
		};

		this.jobPanel = new K.Ext.Panel(options);
		this.jobPanel.add(this.createJobGrid());
		this.jobPanel.add(this.createJobInfoEditPanel());

		return this.jobPanel;
	},

	/**
	 * 职位列表面板
	 */
	createJobGrid : function() {
		var sm = new K.Ext.grid.CheckboxSelectionModel(), options = {
			title : "职位列表",
			iconCls : "p-grid",
			flex : .25,
			store : this.getJobsByOrgCodeStore(),
			sm : sm,
			columns : [ sm, {
				header : "职位编码",
				dataIndex : "cusCode"
			}, {
				header : "职位名称",
				dataIndex : "name"
			} ]
		};

		this.jobGrid = new K.Ext.grid.GridPanel(options);
		this.jobGrid.getSelectionModel().on("rowselect", function(selMode, rowIndex, record) {
			this._loadJobInfoToEditPanel(record.id);
		}, this);

		return this.jobGrid;
	},

	/**
	 * 职位信息编辑面板
	 */
	createJobInfoEditPanel : function() {
		var options = {
			title : "职位信息编辑",
			iconCls : "p-edit",
			bodyStyle : "border-bottom:0px;",
			flex : .55,
			autoScroll : true,
			tbar : {
				xtype : "toolbar",
				cls : "x-toolbar-no-border-r-l",
				hidden : !JobView.purviewUpdateJob() && !JobView.purviewAddJob(),
				items : [ {
					text : "保存",
					iconCls : "save",
					handler : K.createDelegate(this.onSaveJob, this)
				} ]
			}
		};

		this.jobInfoEditPanel = new K.Ext.Panel(options);
		return this.jobInfoEditPanel;
	},

	/**
	 * 按组织机构获取职位普通列表
	 */
	getJobsByOrgCodeStore : function() {
		this.jobsByOrgCodeStore = JobView.createJobsByOrgCodeStore();
		return this.jobsByOrgCodeStore;
	},

	/**
	 * 点击保存按钮,通过判断表单状态执行新增或修改命令
	 */
	onSaveJob : function() {
		var $form = $("form", this.jobInfoEditPanel.getEl().dom);
		var state = $form.attr("state"), eventName = null;
		if (state == K.state.update) {
			if (JobView.purviewUpdateJob()) {
				eventName = "职位-修改";
			} else {
				K.noright(this.jobInfoEditPanel.tbar);
			}
		} else if (state) {
			if (JobView.purviewUpdateJob()) {
				eventName = "职位-新增";
			} else {
				K.noright(this.jobInfoEditPanel.tbar);
			}
		}
		if (eventName) {
			E.fireEvent(eventName, $form);
		}
	},

	/**
	 * 点击修改按钮
	 */
	onUpdateJob : function() {
		var record = this.jobGrid.getSelectionModel().getSelected();
		if (record) {
			this._loadJobInfoToEditPanel(record.id);
		}
	},

	/**
	 * 点击新增按钮,加载新增视图到编辑面板中
	 */
	onAddJob : function() {
		var selectOrgNode = this.orgView.getOrgSelectTreeNode();
		if (selectOrgNode) {
			this.jobInfoEditPanel.setTitle("新增职位信息");
			this.jobInfoEditPanel.load({
				method : "GET",
				url : JobDI.urls.addJob,
				params : {
					orgId : selectOrgNode.id
				}
			});
		} else {
			K.err("请选择职位所属的组织", this.jobGrid.tbar);
		}
	},

	/**
	 * 删除职位信息,派发删除事件
	 */
	onDelJob : function() {
		var records = this.jobGrid.getSelectionModel().getSelections();
		if (records.length != 0) {
			E.fireEvent("职位-删除", records);
		} else {
			K.err("请选择要删除的职位记录", this.jobGrid);
		}
	},

	/**
	 * 组织树事件监听器,加载当前组织的岗位信息
	 */
	onClickOrgTreeNode : function(orgNode) {
		this.jobsByOrgCodeStore.load({
			params : {
				orgCode : orgNode.attributes.code
			}
		});
	},

	/**
	 * 新增职位信息成功事件处理
	 */
	onAddJobSuccess : function(output) {
		var job = output.getParameter("job");
		var newRecord = new (Ext.data.Record.create(PostDI.fields))(job, job.id);
		this.jobGrid.getStore().add(K.Ext.flyRecord(newRecord).set(job).getRecord());
		this._loadJobInfoToEditPanel(job.id);
	},

	/**
	 * 修改职位信息成功事件处理
	 */
	onUpdateJobSuccess : function(output) {
		var job = output.getParameter("job");
		var record = this.jobsByOrgCodeStore.getById(job.id);
		K.Ext.flyRecord(record).set(job).commit();
		K.m("修改职位成功").show(this.jobInfoEditPanel.tbar);
	},

	/**
	 * 删除职位信息成功的事件处理
	 */
	onDelJobSuccess : function(output) {
		var jobIds = output.getParameter("jobIds"), store = this.jobGrid.getStore(), selModel = this.jobGrid.getSelectionModel();
		var delRecs = K.Ext.flyStore(store).autoSelectByDelete(selModel, jobIds);
		var msg = K.m().el(this.jobGrid.getEl().dom);
		if (delRecs.length != 0) {
			msg.msg("成功删除{value}个职位", delRecs.length).show();
			if (store.getCount() == 0) {
				this.onAddJob();
			}
		} else {
			msg.error("删除失败").show();
		}
	},

	// 函数定义
	_loadJobInfoToEditPanel : function(jobId) {
		if (this.jobInfoEditPanel) {
			this.jobInfoEditPanel.setTitle("修改职位信息");
			this.jobInfoEditPanel.load({
				method : "GET",
				url : JobDI.urls.updateJob,
				params : {
					jobId : jobId
				}
			});
		}
	},

	/**
	 * 通过当前选择的组织节点查询职位数据并创建一个下拉选择框
	 */
	createJobSelectCombo : function(config) {
		var orgNode = this.orgView.getOrgSelectTreeNode();
		config.storeBaseParams = {
			orgCode : orgNode.attributes.code
		};
		K.extend(config);
		return JobView.createJobLovCombo(config);
	},

	statics : {
		/**
		 * 创建一个职位下拉列表
		 */
		createJobLovCombo : function(config) {
			var options = {
				triggerAction : 'all',
				valueField : 'id',
				displayField : 'name',
				checkModel : 'single'
			};
			config.store = config.store || JobView.createJobsByOrgCodeStore({
				baseParams : config.storeBaseParams,
				params : config.storeParams
			});
			K.extend(options, config);
			return new Ext.ux.form.LovCombo(options);
		},

		/**
		 * 创建一个按组织id获取职位的数据存储器
		 */
		createJobsByOrgCodeStore : function(config) {
			var options = {
				url : JobDI.urls.getJobsByOrgCode,
				fields : JobDI.fields,
				root : "parameters.jobs"
			};
			if (config) {
				options.baseParams = K.extend(options.baseParams, config.baseParams);
			}
			K.extend(options, config);
			return new K.Ext.data.JsonStore(options);
		}
	}
});